﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using DevFramework.Business.Abstract;
using DevFramework.Entities.Concrete;
using DevFramework.MvcUI.Filters;
using DevFramework.MvcUI.Infrastructure;
using DevFramework.MvcUI.Models;
using DevFramework.MvcUI.Models.ProductModels;
using DevFramework.Entities.Concrete;

namespace DevFramework.MvcUI.Controllers
{
    public class ProductController : BaseController
    {
        private readonly IProductService _productService;
        private readonly IVProductService _vProductService;

        public ProductController(IProductService productService, IVProductService vProductService)
        {
            _productService = productService;
            _vProductService = vProductService;
        }

        

        public int PageSize = 4;
        public ViewResult Index(int? categoryId, int page = 1)
        {
            var products = categoryId.HasValue
                ? _productService.GetByCategory(categoryId.Value)
                : _productService.GetAll();

            return View(new ProductViewModel
            {
                Products = products.Skip((page-1) * PageSize).Take(PageSize).ToList(),
                PagingInfo = new PagingInfo
                {
                    CurrentPage = page,
                    TotalPageCount = (int)Math.Ceiling((decimal)products.Count() / PageSize),
                    BaseUrl = "?page="
                }
            });
        }

        public ActionResult TransactionalInsert()
        {
            var category = new Category
            {
                CategoryName = "New Category",
                Description = "Description"
            };

            var product = new Product
            {
                ProductName = Guid.NewGuid().ToString(),
                QuantityPerUnit = "QuantityPerUnit",
                UnitPrice = 10,
                UnitsInStock = 5
            };

            var added = _productService.Add(product, category);

            SuccessNotification(string.Format("Ürün Kaydınız gerçekleştirilmiştir. Ürün Numarası:{0}", added.ProductId));

            return RedirectToAction("Index");
        }

        public ActionResult List()
        {
            return View(_vProductService.GetAll());
        }

        public ActionResult Add()
        {
            _productService.Add(new Product()
            {
                CategoryId = 1,
                ProductName = "dsdsds",
                QuantityPerUnit = "dsdsdsdsds",
                UnitPrice =32,
                UnitsInStock = 12
            });

            SuccessNotification("OK");

            return RedirectToAction("Index");
        }

        public ActionResult Demo()
        {
            return View(_productService.GetByCategory(1));
        }

        public ActionResult Delete(IEnumerable<int> productIdsToDelete)
        {
            foreach (var id in productIdsToDelete)
            {
                _productService.Delete(_productService.Get(id));
            }

            return RedirectToAction("Demo");
        }

        public ActionResult Date()
        {
            _productService.UpdateDate();

            return new EmptyResult();
        }
    }
}